/*
 *   Copyright (C) 2007 by Andrzej Oczkowicz
 *   andrew_lz@poczta.fm
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */



#include <QtGui>
#include <QTextEdit>
#include <QTextStream>
#include <QCloseEvent>
#include <QFileDialog>

#include "kmediacatalog.h"
#include "kmaster.h"
#include "dbg.h"


kMediaCatalog::kMediaCatalog()
{
  _D << "init";
  setupUi(this);
  createActions();
  createMenus();
  createToolBars();
  createStatusBar();
}

void kMediaCatalog::closeEvent(QCloseEvent *event)
{
}

kMediaCatalog::~kMediaCatalog()
{
}

void kMediaCatalog::createActions()
{
  createAction(fileNewAct, SLOT( fileNew() ), "filenew", tr("New file"), tr("Ctrl+N"), tr("Create new file"));
  createAction(fileOpenAct, SLOT( fileOpen() ), "fileopen", tr("Open file"), tr("Ctrl+O"), tr("Open file"));
  createAction(fileSaveAct, SLOT(fileSave() ), "filesave", tr("Save file"), tr("Ctrl+S"), tr("Seve current file"));
  createAction(fileSaveAsAct, SLOT(fileSaveAs() ), "filesaveas", tr("Save file as..."), tr("Ctrl+Shift+S"), tr("Save current file with new name"));

  createAction(appAboutAct, SLOT(appAbout() ), "filesaveas", tr("Save file as..."), tr("Ctrl+Shift+S"), tr("Save current file with new name"));
  createAction(appAboutQtAct, SLOT(appAboutQt() ), "filesaveas", tr("Save file as..."), tr("Ctrl+Shift+S"), tr("Save current file with new name"));
  createAction(appExitAct, SLOT(appExit() ), "filesaveas", tr("Save file as..."), tr("Ctrl+Shift+S"), tr("Save current file with new name"));

}

void kMediaCatalog::fileNew()
{
  kMaster::getInstance()->newScan("");
}

void kMediaCatalog::fileOpen()
{
}

bool kMediaCatalog::fileSaveAs()
{
}

bool kMediaCatalog::fileSave()
{
}

void kMediaCatalog::createAction(QAction*&action, char*slot, const QString & icon, const QString & name, const QString & shortcut, const QString & tip)
{
  action = new QAction( QIcon( QString(":/actions/%1.png" ).arg( icon ) ), name, this);
  action->setShortcut(shortcut);
  action->setStatusTip(tip);
  connect(action, SIGNAL(triggered()), this, slot ); 
}

void kMediaCatalog::createStatusBar()
{
      statusBar()->showMessage(tr("Ready"));
}

void kMediaCatalog::createToolBars()
{
      fileToolBar = addToolBar(tr("File"));
      fileToolBar->addAction(fileNewAct);
      fileToolBar->addAction(fileOpenAct);
      fileToolBar->addAction(fileSaveAct);
    //  fileToolBar->addAction(acfileSaveAs);


//      editToolBar = addToolBar(tr("Edit"));
//       editToolBar->addAction(cutAct);
//       editToolBar->addAction(copyAct);
//       editToolBar->addAction(pasteAct);
}

void kMediaCatalog::createMenus()
{
      fileMenu = menuBar()->addMenu(tr("&File"));
      fileMenu->addAction(fileNewAct);
      fileMenu->addAction(fileOpenAct);
      fileMenu->addAction(fileSaveAct);
      fileMenu->addAction(fileSaveAsAct);
      fileMenu->addSeparator();
      fileMenu->addAction(appExitAct);

//       editMenu = menuBar()->addMenu(tr("&Edit"));
//       editMenu->addAction(cutAct);
//       editMenu->addAction(copyAct);
//       editMenu->addAction(pasteAct);
// 
      menuBar()->addSeparator();

      helpMenu = menuBar()->addMenu(tr("&Help"));
      helpMenu->addAction(appAboutAct);
      helpMenu->addAction(appAboutQtAct);
}

void kMediaCatalog::appAboutQt()
{
}

void kMediaCatalog::appExit()
{
}

void kMediaCatalog::appAbout()
{
}

void kMediaCatalog::startTree()
{
  treeCatalogs->clear();
}

bool kMediaCatalog::openCatalog(kOutputBasic * out, const QString & name)
{
  if (out){
    out->setCatalogFileName(name);
    buildTree(out);
  }
}

void kMediaCatalog::addNode(kNode * node)
{
//   QTreeWidgetItem *item;
//   if (node)
//     item = new QTreeWidgetItem(  );
//   else
//     item = new QTreeWidgetItem( treeCatalogs );
// 
//   item->setText( "node" );
}

void kMediaCatalog::buildTree(kOutputBasic * out)
{
  if (!out)
    return;
  QTreeWidgetItem *rootItem;
  rootItem = new QTreeWidgetItem( treeCatalogs );
  kNode * n = out->rootNode();
  if (!n)
    return;
  rootItem->setText(0, "Catalog");
  rootItem->setIcon(0, QIcon(":/icons/main16.png") );
  addSubNodes(out, rootItem, n);

//   kNodes * nodes = out->subNodes(n);
//   for (int i=0; i<nodes->size(); i++){
//
//
//   }
//   out->freeNodes(nodes);
//   out->freeNode(n);
}

void kMediaCatalog::addSubNodes(kOutputBasic * out, QTreeWidgetItem * root, kNode * node)
{
  if ( ! node || ! root || ! out )
    return;
  if ( ! node->isDir )
    return;
  QTreeWidgetItem *item;
  item = new QTreeWidgetItem( root );
  item->setText(0, node->name);
  item->setIcon(0, QIcon(":/mime/dir.png") );
/*  else
    item->setIcon(0, QIcon(":/mime/txt.png") );*/
  kNodes * nodes = out->subNodes(node);
  for (int i=0; i<nodes->size(); i++){
    addSubNodes(out, item, nodes->at(i) );
  }
  out->freeNodes(nodes);
}

